package it.bela.pgcdc.editor;

import it.bela.pgcdc.domain.MailingList;
import it.bela.pgcdc.service.MailingListService;

import java.beans.PropertyEditorSupport;

public class MaililngListPropertyEditor extends PropertyEditorSupport {

	private MailingListService mailingListService;
 
	public MaililngListPropertyEditor(MailingListService mailingListService) {
		this.mailingListService = mailingListService;
	}
	
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
    	Long id = null;
    	MailingList mailingList = null;
    	if (!text.equals("-")){
    		id = new Long(text);
    		mailingList = mailingListService.findById(MailingList.class, id);
    	}
        super.setValue(mailingList);
    }
    
    @Override
    public String getAsText() {
    	MailingList mailingList = (MailingList) getValue();
    	String text = null;
    	if (mailingList != null)
    		text = mailingList.getId().toString();
    	return text;
    }
	
}
